package myJs

import myJs.myPkg.jquery._
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom.{Element, Event, document, window}

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import js.Dynamic.{global => g}
import myJs.Implicits._
import myJs.myPkg._
import scala.concurrent.ExecutionContext.Implicits.global
import scalatags.Text.all._

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("Help")
object Help {

  val tableId = "table"

  @JSExport("init")
  def init = {
    refreshTable
    registerEvent
  }

  def refreshTable = {
    val url = g.jsRoutes.controllers.ToolController.getFileData().url.toString
    Ajax.get(url = s"${url}?fileName=columns.txt").map { xhr =>
      val rs = xhr.responseText.toJDJAn
      val columnNames = rs("columnNames").asInstanceOf[js.Array[String]]
      val columns = columnNames.map { columnName =>
        val fmt = columnName
        ColumnOptions.field(columnName).title(columnName).sortable(true)
      }
      val options = TableOptions.data(rs("array")).columns(columns).search(false).pagination(false)
      $(s"#${tableId}").bootstrapTable(options)
    }
  }


  def registerEvent = {
    $(document.body).on("click", s"#back2Top,#myBtn", "", (y: Element, e: Event) => {
      e.preventDefault()
      $("html,body").animate(js.Dictionary("scrollTop" -> 0), "slow")
      false
    })
    $(window).scroll(() => {
      if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        $("#myBtn").show()
      } else {
        $("#myBtn").hide()
      }
    })

  }

}
